package com.yanwq.sayhi.controller.user.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;

import com.alibaba.fastjson.JSONObject;
import com.yanwq.sayhi.MainActivity;
import com.yanwq.sayhi.R;
import com.yanwq.sayhi.base.BaseActivity;
import com.yanwq.sayhi.base.BaseToolbarActivity;
import com.yanwq.sayhi.common.client.OkJsonObjectResponseCallback;
import com.yanwq.sayhi.common.client.OkRequestParams;
import com.yanwq.sayhi.common.client.OkRestClient;
import com.yanwq.sayhi.common.constant.RegexConstant;
import com.yanwq.sayhi.common.event.UserEvent;
import com.yanwq.sayhi.controller.user.UserMgr;
import com.yanwq.sayhi.widget.MEditText;
import com.yanwq.sayhi.widget.MToast;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

public class LoginActivity extends BaseToolbarActivity {

    @BindView(R.id.login_username)
    MEditText etUsername;
    @BindView(R.id.login_password)
    MEditText etPassword;

    @OnClick(R.id.login_register)
    void register() {
        Intent intent = new Intent(getBaseContext(), RegisterActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        setContentView(R.layout.activity_login);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        // This example uses decor view, but you can use any visible view.
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        decorView.setSystemUiVisibility(uiOptions);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_send, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.send:
                login();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }

        return true;
    }

    @SuppressWarnings("unused")
    public void onEvent(UserEvent event) {
        switch (event.getEventType()) {
            case UserEvent.LOGIN:
                finish();
                break;
        }
    }

    private void login() {
        if (!Pattern.matches(RegexConstant.username, etUsername.getText().toString())) {
            MToast.showShort(this, "用户名格式错误");
            return;
        }

        if (!Pattern.matches(RegexConstant.password, etPassword.getText().toString())) {
            MToast.showShort(this, "密码格式错误");
            return;
        }

        OkRequestParams params = new OkRequestParams();
        params.put("username", etUsername.getText().toString());
        params.put("password", etPassword.getText().toString());
        OkRestClient.getInstance().get(this,
                "/user/login",
                params,
                new OkJsonObjectResponseCallback() {
                    @Override
                    public void onStart() {
                        showProgressDialog("正在登录");
                    }

                    @Override
                    public void onStop() {
                        cancelProgressDialog();
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        UserMgr.save(jsonObject);
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        //ChatService.start(LoginActivity.this);
                        finish();
                    }
                });
    }
}
